//
//  UIViewController+DesignPattern.m
//  CKDesignPattern
//
//  Created by 开发 on 2017/6/12.
//  Copyright © 2017年 开发. All rights reserved.
//

#import "UIViewController+DesignPattern.h"
#import <objc/runtime.h>


@implementation UIViewController (DesignPattern)

+ (void)swizzleMethod:(SEL)originalSelector andAnotherSelecor:(SEL)swizzledSelector {
    Class class = [self class];
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

+ (void)load{
    [UIViewController swizzleMethod:@selector(viewDidLoad) andAnotherSelecor:@selector(patternViewDidLoad)];
}

- (void)patternViewDidLoad{
    [self patternViewDidLoad];
    switch (self.designPatternType) {
        case patternCommander:{
            [self dealWithCommanderPattern];
        }
            break;
            
        default:
            break;
    }
    NSLog(@"self.designPatternType:%ld",self.designPatternType);
    
}

- (void)setDesignPatternType:(DesignPatternCellType)designPatternType{
    objc_setAssociatedObject(self, @selector(designPatternType), @(designPatternType), OBJC_ASSOCIATION_COPY);
}

- (DesignPatternCellType)designPatternType{
    return [objc_getAssociatedObject(self, _cmd)  integerValue];
}

- (void)dealWithCommanderPattern{
    self.view.backgroundColor = [UIColor redColor];
    
}


@end
